﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using GemWorkshop.Formats.Nitro;

namespace GemWorkshop.Pokemon
{
    public partial class PokeEditor : Form
    {
        private Narc nr, enr, mnr;
        private PokemonLoader pl;
        private int currentPoke, currentEvo;

        public PokeEditor()
        {
            InitializeComponent();

            nr = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "PokemonData"));
            enr = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "EvolutionData"));
            mnr = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "MovesetData"));

            pl = new PokemonLoader(nr, enr, mnr);
            listPokemon.Items.Clear();
            for (int i = 0; i < nr.FileCount; i++)
            {
                string s = pl.GetName(i);
                listPokemon.Items.Add(s);
                cEvoPoke.Items.Add(s);
            }

            bSave.Enabled = false;
        }

        private void bSave_Click(object sender, EventArgs e)
        {

        }

        private void bCross_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listPokemon_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                currentPoke = listPokemon.SelectedIndex;
                pl.LoadPokemon(currentPoke);

                #region Set Base Stats

                tHp.Text = pl.baseStats.HP.ToString();
                tAtk.Text = pl.baseStats.Attack.ToString();
                tDef.Text = pl.baseStats.Defense.ToString();
                tSpd.Text = pl.baseStats.Speed.ToString();
                tSpAtk.Text = pl.baseStats.SpAttack.ToString();
                tSpDef.Text = pl.baseStats.SpDefense.ToString();

                tHp2.Text = pl.baseStats.EVHp.ToString();
                tAtk2.Text = pl.baseStats.EVAttack.ToString();
                tDef2.Text = pl.baseStats.EVDefense.ToString();
                tSpd2.Text = pl.baseStats.EVSpeed.ToString();
                tSpAtk2.Text = pl.baseStats.EVSpAttack.ToString();
                tSpDef2.Text = pl.baseStats.EVSpDefense.ToString();

                cType1.SelectedIndex = pl.baseStats.Type1;
                cType2.SelectedIndex = pl.baseStats.Type2;

                cAbility1.SelectedIndex = pl.baseStats.Ability1;
                cAbility2.SelectedIndex = pl.baseStats.Ability2;

                cItem1.SelectedIndex = pl.baseStats.Item1;
                cItem2.SelectedIndex = pl.baseStats.Item2;

                tExp.Text = pl.baseStats.BaseXp.ToString();
                cGrowthRate.SelectedIndex = pl.baseStats.XpType;

                tCatch.Text = pl.baseStats.CatchRate.ToString();
                tRun.Text = pl.baseStats.SafariRunRate.ToString();

                cColor.SelectedIndex = pl.baseStats.DexColor;

                cEgg1.SelectedIndex = pl.baseStats.EggType1;
                cEgg2.SelectedIndex = pl.baseStats.EggType2;
                tHatch.Text = (pl.baseStats.EggCycles + 1).ToString();

                tHappy.Text = pl.baseStats.BaseHappiness.ToString();

                if (pl.baseStats.HasGenders)
                {
                    trkGen.Visible = true;
                    trkGen.Value = pl.baseStats.GenderRatio;
                    bGender.Text = "REMOVE";
                }
                else
                {
                    trkGen.Visible = false;
                    trkGen.Value = 0;
                    bGender.Text = "ADD";
                    lblGens.Text = "No gender!";
                }

                #endregion

                #region Set Evolutions
                listEvolutions.Items.Clear();
                for (int i = 0; i < 7; i++)
                {
                    listEvolutions.Items.Add((i + 1).ToString() + " - " + pl.GetEvolutionName(pl.evolutions[i].Type));
                }

                cEvoType.SelectedIndex = 0;
                tEvoParamLvl.Text = "0";
                cEvoParamAtk.SelectedIndex = 0;
                cEvoParamItm.SelectedIndex = 0;
                cEvoPoke.SelectedIndex = 0;
                #endregion

                listMoves.Items.Clear();
                for (int i = 0; i < pl.moveset.Length; i++)
                {
                    listMoves.Items.Add(string.Format("{0} - {1}", pl.moveset[i].Level, pl.GetMoveName(pl.moveset[i].Value)));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region Base Stats

        private void trkGen_ValueChanged(object sender, EventArgs e)
        {
            double d = Percentage(254, trkGen.Value);
            lblGens.Text = d.ToString("0.00") + "% : " + (100 - d).ToString("0.00") + "%";
        }

        private double Percentage(double max, int value)
        {
            //Calculate a percentage
            double d = 0.0;
            d = value * 100 / max;
            return d;
        }

        private void tHatch_TextChanged(object sender, EventArgs e)
        {
            if (tHatch.TextLength > 0)
            {
                int cycles = int.Parse(tHatch.Text);
                lblHatchInfo.Text = "Cycles - " + (cycles * 256).ToString() + " Steps";
            }
        }

#endregion

        private void listEvolutions_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentEvo = listEvolutions.SelectedIndex;

            ushort type = pl.evolutions[currentEvo].Type;
            cEvoType.SelectedIndex = type;
            cEvoPoke.SelectedIndex = pl.evolutions[currentEvo].Target;

            tEvoParamLvl.Text = "0";
            cEvoParamAtk.SelectedIndex = 0;
            cEvoParamItm.SelectedIndex = 0;

            switch (type)
            {
                // Level up evolutions...
                case 4:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                    tEvoParamLvl.Text = pl.evolutions[currentEvo].Parameter.ToString();
                    break;
                // Item
                case 6:
                case 7:
                case 16:
                case 17:
                case 18:
                case 19:
                    cEvoParamItm.SelectedIndex = pl.evolutions[currentEvo].Parameter;
                    break;
                // Attack
                case 20:
                    cEvoParamAtk.SelectedIndex = pl.evolutions[currentEvo].Parameter;
                    break;
            }
        }
    }
}
